
// Copyright (C) 2010-2011 Luca Piccioni
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows.Forms;

using Derm.Render;

using ShaderGen.Ui;

namespace ShaderGen
{
	/// <summary>
	/// Shaders generator.
	/// </summary>
	class ShaderGenerator
	{
		#region Application Entry Point
		
		/// <summary>
		/// Application entry point.
		/// </summary>
		/// <param name="args">
		/// A <see cref="System.String[]"/> representing the application command line.
		/// </param>
		[STAThread()]
		public static void Main(string[] args)
		{
			if (args.Length > 0) {
				// Load XML configuration
				ShaderLibraryConfiguration config = null;
				string xmlPath = args[0];

				try {
					config = ShaderLibraryConfiguration.Load(xmlPath);
				} catch (Exception e) {
					Exception exceptionIter = e.InnerException;

					Console.WriteLine("Failed to load shader library configuration: {0}", e.Message);
					while (exceptionIter != null) {
						Console.WriteLine("  {0}", exceptionIter.Message);
						exceptionIter = exceptionIter.InnerException;
					}
					Environment.Exit(-1);
				}

				AppWorkingDir = Path.GetDirectoryName(xmlPath);
				Directory.SetCurrentDirectory(Path.GetDirectoryName(xmlPath));

				// Generate C# sources
				config.Generate();
			} else {
				string workingDir = Directory.GetCurrentDirectory();

				if (Regex.Match(workingDir, @"bin(/|\\)(x86|x64)(/|\\)(Debug|Release)(/|\\)?").Success)
					Directory.SetCurrentDirectory(Path.Combine(workingDir, "../../../"));

				using (MainWindow mainWindow = new MainWindow()) {
						Application.EnableVisualStyles();
						Application.Run(mainWindow);
				}
			}

			return;

			
		}

		internal static string AppWorkingDir;

		#endregion
	}
}
